import { AEvent } from "../../../../../Core/Module/EventSystem/IEvent";
import { Log } from "../../../../../Core/Module/Log/Log";
import { EventAttribute, GetParentTypeOf, GetTypeOf } from "../../../../../Decorator/Decorator";
import { ETTask } from "../../../../../Support/Share/Lib/Async/ETTask";
import { NetInnerComponentOnRead } from "../../../../Model/Server/Module/Message/NetInnerComponent";
import { IActorResponse, IActorRequest, IActorMessage } from "../../../../Model/Share/Module/Actor/IActorMessage";
import { ActorHandleHelper } from "../../Module/Actor/ActorHandleHelper";

@EventAttribute(NetInnerComponentOnRead)
export class NetInnerComponentOnReadEvent extends AEvent<NetInnerComponentOnRead>
{
    public override async Run(args: NetInnerComponentOnRead): Promise<void> {
        // console.log(`NetInnerComponentOnReadEvent.Run`);

        try {
            let actorId = args.ActorId;
            let message = args.Message;

            // console.log(GetTypeOf(message));
            // console.log(message);
            // console.log(GetTypeOf(IActorResponse));
            // console.log(GetTypeOf(IActorRequest));
            // console.log(GetTypeOf(IActorMessage));

            // 收到actor消息,放入actor队列
            switch (GetParentTypeOf(message)) {
                case GetTypeOf(IActorResponse):
                    {
                        ActorHandleHelper.HandleIActorResponse(message as IActorResponse);
                        break;
                    }
                case GetTypeOf(IActorRequest):
                    {
                        await ActorHandleHelper.HandleIActorRequest(actorId, message as IActorRequest);
                        break;
                    }
                case GetTypeOf(IActorMessage):
                    {
                        await ActorHandleHelper.HandleIActorMessage(actorId, message as IActorMessage);
                        break;
                    }
            }
        } catch (e) {
            Log.Error(`InnerMessageDispatcher error: ${args.Message}\n${e}`);
        }
        await ETTask.CompletedTask;
    }
}